/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Oceanside
{
    public class CoinDispenser
    {


        /// <summary>
        /// Init the Crane Coin Dispenser
        /// </summary>
        /// <returns></returns>
        public static bool Init()
        {
            bool isReady = false;

            try
            {

            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return isReady;
        }

        /// <summary>
        /// Dispenser the amount of change (less than $1.00; i.e. 1->99)
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
        public static void DispenseAmount(int amount)
        {
            try
            {
                bool wasSent = false;
                if (MainWindow.Singleton.CoinUSB.SpecifiedDevice != null)
                {
                    byte[] data = new byte[59];

                    int indx = 0;
                    try
                    {
                        ///Reset the machine status.
                        data[indx++] = 1;
                        data[indx++] = ((byte)0x04);
                        data[indx++] = ((byte)0x43);
                        data[indx++] = ((byte)0x72);
                        data[indx++] = ((byte)0x05);
                        data[indx++] = ((byte)0x00);
                        data[indx++] = ((byte)0x00);
                        data[indx++] = ((byte)0x00);
                        MainWindow.Singleton.CoinUSB.SpecifiedDevice.SendData(data);

                        ///Prepare to send the change amount
                        indx = 0;

                        ///Found that this should be set after using a bus sniffer.
                        data[indx++] = 1;

                        ///Header
                        data[indx++] = ((byte)0x04);
                        data[indx++] = ((byte)0x43);
                        data[indx++] = ((byte)0x02);

                        ///Change
                        byte upper = ((byte)GetAsciiIntOfInt(((int)(amount/10))));
                        byte lower = ((byte)GetAsciiIntOfInt(((int)(amount % 10))));

                        data[indx++] = upper;
                        data[indx++] = lower;

                        ///End Message
                        data[indx++] = ((byte)0x03);

                        ///Checksum
                        data[indx++] = ((byte)((upper^lower)^0x03));
                        MainWindow.Singleton.CoinUSB.SpecifiedDevice.SendData(data);

                        wasSent = true;
                    }
                    catch (Exception ex)
                    {
                        Logging.LogException(ex);
                    }
                }

                if (!wasSent)
                {
                    UMD.ShowGuiMessageSTASafe_NOTHROW("The system was unable to dispense the customer's coins.");
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        public static int GetDispensedAmount()
        {
            try
            {
                if (MainWindow.Singleton.CoinUSB.SpecifiedDevice != null)
                {
                    byte[] data = new byte[59];

                    int indx = 0;
                    try
                    {
                        ///Reset the machine status.
                        data[indx++] = 1;
                        data[indx++] = ((byte)0x04);
                        data[indx++] = ((byte)0x43);
                        data[indx++] = ((byte)0x64);
                        MainWindow.Singleton.CoinUSB.SpecifiedDevice.SendData(data);
                    }
                    catch (Exception ex)
                    {
                        Logging.LogException(ex);
                    }
                }
                return 0;
            }
            catch (Exception e)
            {
                Logging.LogException(e);
                return 0;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private static int GetAsciiIntOfInt(int i)
        {
            switch(i)
            {
                case 0:
                    return Convert.ToInt32('0');
                case 1:
                    return Convert.ToInt32('1');
                case 2:
                    return Convert.ToInt32('2');
                case 3:
                    return Convert.ToInt32('3');
                case 4:
                    return Convert.ToInt32('4');
                case 5:
                    return Convert.ToInt32('5');
                case 6:
                    return Convert.ToInt32('6');
                case 7:
                    return Convert.ToInt32('7');
                case 8:
                    return Convert.ToInt32('8');
                case 9:
                    return Convert.ToInt32('9');
                default:
                    return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public enum ENUM_CAN_STATUS
        {
            PAID_IN_FULL,
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ENUM_CAN_STATUS GetCanisterStatus()
        {
            return ENUM_CAN_STATUS.PAID_IN_FULL;
        }
    }
}
